package pl.ydp.ivy.flex.ide;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

import org.apache.ivy.Ivy;
import org.apache.ivy.core.settings.IvySettings;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;

import pl.ydp.ivy.IIvyProvider;
import pl.ydp.ivy.flex.ide.preferences.PreferenceConstants;

public class ProjectIvyProvider {
	
	public IIvyProvider getProjectIvyProvider( IProject project ) {
		return new IvyProvider(project);
	}
	
	public boolean isIvyPresent( IProject project )  {
		IFile ivyFile = project.getFile( "ivy.xml" );
		
		return ivyFile.exists();
	}
	
	public File getIvyFile( IProject project ) {
		return project.getFile( "ivy.xml" ).getLocation().toFile();
	}
	
	public String getArtifactPath( IProject project ) {
		String artifactPath = 
			Activator.getDefault().
				getPreferenceStore().getString( PreferenceConstants.P_STRING );
		
		return getArtifactBaseFolder(project)+"/"+
			((artifactPath==null || artifactPath.trim().length()==0) ?
				"[artifact].[ext]" : artifactPath);
	}
	
	public Ivy createIvy( IProject project ) throws ParseException, IOException {
		Ivy ivy = Ivy.newInstance( getIvySettings(project) );
		ivy.getLoggerEngine().pushLogger( new ConsoleMessageLogger(2) );
		
		return ivy;
	}
	
	public String getArtifactBaseFolder( IProject project ) {
		return "ivy-lib";
	}
	
	public IvySettings getIvySettings( IProject project ) throws IOException, ParseException {
		String path = Activator.getDefault().getPreferenceStore().getString( PreferenceConstants.P_PATH );
		
		File config;
		if ( path!=null && path.trim().length()>0 ) {
			config = new File(path);
		} else {
			File lConfig = new File(
					project.getWorkspace().getRoot().getLocation().toFile(),
					"ivysettings.xml" );
			if ( lConfig.exists() )
				config = lConfig;
			else 
				config = new File( new File( System.getProperty("user.home")), ".ivy2/ivysettings.xml" );
		}
		
		IvySettings settings = new IvySettings();
		settings.load( config.toURI().toURL() );
		settings.setBaseDir( project.getLocation().toFile() );
		
		return settings;
	}
	
	class IvyProvider implements IIvyProvider {
		IProject project;
		Ivy ivy;
		
		public IvyProvider(IProject project) {
			this.project = project;
		}
		public Ivy getIvy() throws ParseException,
				IOException {
			if ( ivy==null )
				ivy = ProjectIvyProvider.this.createIvy(project);
			return ivy;
		}
		public String getArtifactPath() {
			return ProjectIvyProvider.this.getArtifactPath(project);
		}
		public File getIvyFile() {
			return ProjectIvyProvider.this.getIvyFile(project);
		}
		public IvySettings getIvySettings() throws IOException,
				ParseException {
			return ivy==null ? 
					ProjectIvyProvider.this.getIvySettings(project) :
					ivy.getSettings();
		}
		public boolean isIvyPresent() {
			return ProjectIvyProvider.this.isIvyPresent(project);
		}
		public String getArtifactBaseFolder() {
			return ProjectIvyProvider.this.getArtifactBaseFolder(project);
		}
	}
}
